package Test_Performance;

/**
 * @author wly
 * @version V1.0.0
 * @date 2023/1/21 13:35
 */
public class LinearSearch {
    private LinearSearch(){}

    public static <E> int search(E[] data, E target){
        for (int i = 0; i < data.length; i++) {
            if (data[i].equals(target)){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        int[] dataSize = {10000, 100000};
        for (int n : dataSize) {
            Integer[] data = ArrayGenerator.generateOrderArray(n);
            long startTime = System.nanoTime();
            for (int k = 0; k < 100; k++){
                LinearSearch.search(data, n);
            }
            long endTime = System.nanoTime();
            double time = (endTime - startTime) / 1000000000.0;
            System.out.println("n = " + n + ", 100 runs : " + time + "s");
        }
    }
}
